package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.common.constant.MessageConstant;
import com.atguigu.common.constant.RedisMessageConstant;
import com.atguigu.common.entity.Result;
import com.atguigu.service.MemberService;
import com.atguigu.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

@RestController
@RequestMapping("/login")
public class MemberController {

    @Autowired
    JedisPool jedisPool;
    @Reference
    MemberService memberService;

    /*
    用户登录
     */
    @PostMapping("/check")
    public Result checkLogin(@RequestBody LoginVo loginVo){
        Jedis jedis = jedisPool.getResource();
        //验证码是否正确
        String telephone = loginVo.getTelephone();
        String validateCode = loginVo.getValidateCode();
        String redisCode = jedis.get(RedisMessageConstant.SENDTYPE_LOGIN + ":" + telephone);
        if (redisCode==null){
            return new Result(false,"验证码过期,请重新发送");
        }
        if (redisCode!=validateCode){
            return new Result(false,"验证码输入错误，请重新发送");
        }
        //保存用户登录信息
        memberService.login(loginVo);
        return null;
    }


}
